-- weaken_mon
-- created by panyl
-- 通用的削弱目标类技能

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 必须是怪物
        if source.type ~= OBJECT_TYPE_MONSTER then
            return;
        end

        -- 如果免疫了该技能
        if SkillM.immunitySkill(source, target, skillId) then
            SkillM.getSequence(target):immunity(source, target, skillId);
            return {};
        end

        local effectRound = -1;
        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound;
        elseif para[4] > 0 then
            effectRound = round + para[4];
        end

        -- 攻击
        local attack = source:getAttack();
        -- 被对方百分比削弱
        local prop = PropM.combine(target, "weak_enemy", "attack");
        attack = PropM.apply(prop, attack);

        -- 被对方绝对值削弱
        prop = PropM.combine(target, "weak_enemy2", "attack");
        attack = PropM.apply(prop, attack);

        -- 威力
        local force = para[3];

        local v = math.modf(force * attack / 1000);
        if para[1] == PropM.getPropId("poisoned") then
            local floor = DungeonM.currentLayer();
            local dungeonId = DungeonM.getDungeonId();
            if dungeonId == nil then
                dungeonId = ME.user.dbase:query("currentDungeon");
            end
            local layerFix = DungeonAreaM.query(dungeonId,"layer_fix");
            v = math.modf(force * attack * 60 / floor / (60 + layerFix)) + 1;
        end

        v = math.max(1, v);

        prop = { para[1], para[2], v, effectRound, }

        local rule = PropM.query(para[1], "rule");
        local condition = {
            ["prop"] = prop,
            ["end_round"] = effectRound,
        };

        -- 附加状态
        CombatStatusM.applyStatus(target, rule, condition);

        -- 返回作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), arr[2], tonumber(arr[3]), tonumber(arr[4] or -1), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 魔力
        local attack = source:getAttack();

        -- 威力
        local force = para[3];

        local v = math.modf(force * attack / 1000);

        v = math.max(1, v);

        -- 效果
        desc = string.gsub(desc, "{weaken_mon}", math.abs(v));

        -- 回合数
        desc = string.gsub(desc, "{round}", para[4]);

        return desc;
    end,
};
